# models

from django.db import models

class Identity(models.Model):
    url = models.CharField(max_length=255)
    name = models.CharField(max_length=255, null=True)
    def __unicode__(self):
        return self.name
    @staticmethod
    def get_by_url(openid):
        url = str(openid)
        if url.startswith('http://') or url.startswith('https://'):
            url = url[url.index('/')+2:]
        while url.endswith('/'):
            url = url[:-1]

        identity = Identity.objects.get_or_create(url=url)[0]
        if not (isinstance(openid, type(u'')) or isinstance(openid, type(''))):
                identity.name = openid.sreg.get('nickname')
        identity.save()
        return identity


class Reservation(models.Model):
    identity = models.ForeignKey(Identity)
    event = models.IntegerField()
    status = models.IntegerField(null=True)
    YES = 1
    NO = 0
    MAYBE = 2
    def get_status(self):
        if self.status == Reservation.YES: 
            return 'yes'
        if self.status == Reservation.NO:
            return 'no'
        if self.status == Reservation.MAYBE:
            return 'maybe'
    
    def __unicode__(self):
        return unicode(self.identity) + unicode(self.event) + unicode(self.status)
        
